package cn.flt.lucia.controllers;

import cn.flt.lucia.config.*;
import cn.flt.lucia.utils.Hash;
import lombok.extern.slf4j.Slf4j;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.ProposalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author : 州长在手 2020/9/10 上午11:52
 * 1. 测试数据加载
 */
//@Slf4j
//@RestController
//@CrossOrigin
//@RequestMapping(value = "/sub")
public class SubstrateController {
    private final String CHAINCODE_NAME = "htt";

    private final EnvRunner envRunner;

    public SubstrateController(EnvRunner envRunner) {
        this.envRunner = envRunner;
    }


    /**
     * echo "========================Start  queryMember========================"
     * peer chaincode query -C myc -n Log -c '{"Args":["queryMember","10000001"]}'
     * echo "========================queryMember End========================"
     *
     * @return Object
     */
    @PostMapping("/queryMember")
    public Object envRunnerPostQueryMember(@RequestParam("id") String id) throws ProposalException, IOException, InvalidArgumentException {
        String[] args = new String[]{id};
        return envRunner.getFm().invoke(CHAINCODE_NAME, "queryMember", args);
    }

    /**
     * echo "========================Start initMember========================"
     * peer chaincode invoke -C myc -n Log -c '{"Args":["initMember","{\"value\":\"VALUE\",\"key\":\"10000001\"}"]}'
     * echo "========================initMember End=========================="
     * sleep 5s
     *
     * @return Object
     */
    @PostMapping("/initMember")
    public Object envRunnerPostInitMember(@RequestParam("id") String id) throws ProposalException, IOException, InvalidArgumentException {

        String[] args = new String[]{"{\"value\":\"VALUE\",\"key\":\"" + id + "\"}"};
        return envRunner.getFm().invoke(CHAINCODE_NAME, "initMember", args);
    }

}
